/**
 * Vivien2
 *
 * Copyright (c) 2009 Alexei (aka Lancelot) Samsonov.
 * E-mail: alexey.v.samsonov@gmail.com; 
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Authors: Alexei Samsonov (aka Lancelot), alexey.v.samsonov@gmail.com
 */

/**
 * Date: 
 */


module obj.constant;


import std.regexp;
import std.date;
import std.string;

import environment;





abstract class Const
{
	private RegExp _reg;
	private char[] _by;
	private char[] _value;

	
	this(char[] s)
	{
		_value = s;
		_by = getReplacement();
	}
	
	
	char[] parse()
	{
		return _reg.replace(_value, _by);
	}


	char[] getReplacement()
	{
		char[] s;
		return s;
	}
}




class SysInfo : Const
{
	this(char[] s)
	{
		super(s);
		_reg = new RegExp("{SysInfo}", "mg");
	}
	
	
	char[] getReplacement()
	{
		char[] s;
		char[] env_html;
		Environment e = new Environment();

		char[][] env_arr = [
			"SERVER_SOFTWARE",
			"GATEWAY_INTERFACE",
			"SERVER_ADMIN",
			"DOCUMENT_ROOT",
			"SERVER_NAME",
			"HTTP_HOST",
			"HTTP_ACCEPT",
			"HTTP_ACCEPT_CHARSET",
			"HTTP_ACCEPT_ENCODING",
			"HTTP_ACCEPT_LANGUAGE",
			"HTTP_USER_AGENT",
			"HTTP_REFERER",
			"HTTP_CONNECTION",
			"SERVER_PORT",
			"REMOTE_HOST",
			"REMOTE_PORT",
			"REMOTE_ADDR",
			"REMOTE_USER",
			"SERVER_PROTOCOL",
			"REQUEST_METHOD",
			"REQUEST_URI",
			"REQUEST_IDENT",
			"AUTH_TYPE",
			"CONTENT_TYPE",
			"CONTENT_LENGTH",
			"SCRIPT_NAME",
			"SCRIPT_FILENAME",
			"QUERY_STRING",
			"PATH_INFO",
			"PATH_TRANSLATED"
		];


		foreach(char[] v; env_arr)
		{
			env_html ~= "<tr><td>" ~ v ~ "</td><td>" ~ e.get(v) ~ "</td></tr>\n";
		}



		s ~= "\n\n" ~ 
			 "<div class=\"widget\">\n" ~
			 "<p><strong>CGI Environments:</strong></p>\n" ~
			 "<table border=\"1\" cellpadding=\"4\" cellspacing=\"0\">\n" ~
			 env_html ~
			 "</table>\n" ~
			 "</div>\n" ~
			 "\n\n";

		return s;
	}
}




class Year : Const
{
	this(char[] s)
	{
		super(s);
		_reg = new RegExp("{Year}", "mg");
	}
	
	
	char[] getReplacement()
	{
		char[] s;
		long time;
		int year;

		time = getUTCtime();
		year = YearFromTime(time);
		s = std.string.toString(year);

		return s;
	}
}
